package com.duobaoyu.dby.supply.api.controller;

import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.common.security.annotation.Inner;
import com.duobaoyu.dby.supply.api.service.AfterSaleBizService;
import com.duobaoyu.dby.supply.biz.constant.OperateSourceConstants;
import com.duobaoyu.dby.supply.common.biz.remote.dto.refund.*;
import com.duobaoyu.dby.supply.common.biz.remote.to.refund.*;
import com.pig4cloud.plugin.idempotent.annotation.Idempotent;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 售后
 *
 * @author fengshaoyu
 * @version 1.0.0
 * @date 2023/9/26 10:15
 */
@Inner
@RestController
@RequestMapping("/api/after-sale")
@RequiredArgsConstructor
public class AfterSaleController {
	private final AfterSaleBizService afterSaleBizService;

	/**
	 * 批量查询商品售后权益
	 * @param afterSaleAttributes
	 * @return
	 */
	@ApiOperation("批量查询商品售后权益")
	@PostMapping("/attributes")
	public R<AfterSaleAttributesTO> queryAfterSaleAttributes(@Validated @RequestBody AfterSaleAttributesDTO afterSaleAttributes) {
		return R.ok(afterSaleBizService.queryAfterSaleAttributes(afterSaleAttributes));
	}


	@ApiOperation("售后申请")
	@PostMapping("/apply")
	@Idempotent(key = "'refund:create:' + #afterSaleApply.supplyTradeSn", delKey = true)
	public R<ApplyAfterSaleTO> applyAfterSale(@Validated @RequestBody AfterSaleApplyDTO afterSaleApply) {
		return R.ok(afterSaleBizService.createAfterSale(afterSaleApply));
	}


	/**
	 * 查询客户寄回地址
	 * @param afterSaleAddress
	 * @return
	 */
	@ApiOperation("查询客户寄回地址")
	@PostMapping("/address")
	public R<AfterSaleAddressTO> queryAfterSaleAddress(@Validated @RequestBody AfterSaleAddressDTO afterSaleAddress) {
		return R.ok(afterSaleBizService.queryAfterSaleAddress(afterSaleAddress));
	}


	@ApiOperation("填写运单信息")
	@PostMapping("/wayBill")
	@Idempotent(key = "'refund:way:bill:' + #afterSaleWayBill.afterSaleSn", delKey = true)
	public R<AfterSaleWayBillTO> createAfterSaleWayBill(@Validated @RequestBody AfterSaleWayBillDTO afterSaleWayBill) {
		return R.ok(afterSaleBizService.createAfterSaleWayBill(afterSaleWayBill));
	}

	/**
	 * 取消售后申请
	 * @param cancelAfterSale
	 * @return
	 */
	@ApiOperation("取消售后申请")
	@PostMapping("/cancel")
	public R<CancelAfterSaleApplyTO> cancelAfterSaleApply(@Validated @RequestBody CancelAfterSaleDTO cancelAfterSale) {
		cancelAfterSale.setOperateSource(OperateSourceConstants.OPERATION_BUSINESS);
		return R.ok(afterSaleBizService.cancelAfterSale(cancelAfterSale));
	}

	/**
	 * 确认售后完成
	 * @param confirmAfterSale
	 * @return
	 */
	@ApiOperation("确认售后服务")
	@PostMapping("/confirm")
	public R<ConfirmAfterSaleTO> confirmAfterSale(@Validated @RequestBody ConfirmAfterSaleDTO confirmAfterSale) {
		return R.ok(afterSaleBizService.confirmAfterSale(confirmAfterSale));
	}

	/**
	 * 查询售后物流公司
	 * @param afterSaleLogistics
	 * @return
	 */
	@ApiOperation("查询售后物流公司（自行寄件支持物流公司）")
	@PostMapping("/logistics")
	public R<AfterSaleLogisticsTO> queryAfterSaleLogistics(@Validated @RequestBody AfterSaleLogisticsDTO afterSaleLogistics) {
		return R.ok(afterSaleBizService.queryAfterSaleLogistics(afterSaleLogistics));
	}

	/**
	 * 售后回查接口
	 * @param afterSaleQuery
	 * @return
	 */
	@ApiOperation("售后回查接口")
	@GetMapping("/query")
	public R<ApplyAfterSaleDetailTO> queryAfterSale(@Validated AfterSaleQueryDTO afterSaleQuery) {
		return R.ok(afterSaleBizService.queryAfterSale(afterSaleQuery));
	}

	/**
	 * 上传售后凭证
	 * @param orderSn 订单编码
	 * @param file
	 * @return
	 */
	@ApiOperation("上传售后凭证")
	@PostMapping("/uploadAfterSaleVoucher/{orderSn}")
	public R<String> uploadAfterSaleVoucher(@NotBlank(message = "实物详情订单号不能为空") @PathVariable("orderSn") String orderSn,
											@NotNull(message = "图片文件不能为空") @RequestPart("file") MultipartFile file) {
		return R.ok(afterSaleBizService.uploadAfterSaleVoucher(orderSn, file));
	}
}
